package com.beidouin.sensortest;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.friendlyarm.AndroidSDK.GPIOEnum;
import com.friendlyarm.AndroidSDK.HardwareControler;

public class GPIOActivity extends AppCompatActivity implements Runnable,Spinner.OnItemSelectedListener,View.OnClickListener,CheckBox.OnCheckedChangeListener {

    private static boolean gpioOUT=true;
    private static boolean gpioIN=false;
    private static boolean gpioHIGH=true;
    private static boolean gpioLOW=false;
    LinearLayout[] gpioLayout =new LinearLayout[4];
    Button[] gpioButton =new Button[4];
    TextView[] gpioView =new TextView[4];
    Spinner[] gpioSpinner =new Spinner[4];
    CheckBox[] gpioBox =new CheckBox[4];
    boolean[] gpioSta =new boolean[4];
    boolean[] gpioDir =new boolean[4];
    boolean[]gpioInUsed =new boolean[4];
    int[] lastgpioInSta =new int[4];

    Handler handler;
    Thread t = new Thread(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpio);

        gpioLayout[0]= (LinearLayout) findViewById(R.id.gpiolin_1);
        gpioLayout[1]= (LinearLayout) findViewById(R.id.gpiolin_2);
        gpioLayout[2]= (LinearLayout) findViewById(R.id.gpiolin_3);
        gpioLayout[3]= (LinearLayout) findViewById(R.id.gpiolin_4);
        gpioButton[0]= (Button) findViewById(R.id.gpiobtn_1);
        gpioButton[1]= (Button) findViewById(R.id.gpiobtn_2);
        gpioButton[2]= (Button) findViewById(R.id.gpiobtn_3);
        gpioButton[3]= (Button) findViewById(R.id.gpiobtn_4);
        gpioView[0]= (TextView) findViewById(R.id.gpiosta_1);
        gpioView[1]= (TextView) findViewById(R.id.gpiosta_2);
        gpioView[2]= (TextView) findViewById(R.id.gpiosta_3);
        gpioView[3]= (TextView) findViewById(R.id.gpiosta_4);
        gpioSpinner[0]= (Spinner) findViewById(R.id.gpiodir_1);
        gpioSpinner[1]= (Spinner) findViewById(R.id.gpiodir_2);
        gpioSpinner[2]= (Spinner) findViewById(R.id.gpiodir_3);
        gpioSpinner[3]= (Spinner) findViewById(R.id.gpiodir_4);
        gpioBox[0]= (CheckBox) findViewById(R.id.gpiock_1);
        gpioBox[1]= (CheckBox) findViewById(R.id.gpiock_2);
        gpioBox[2]= (CheckBox) findViewById(R.id.gpiock_3);
        gpioBox[3]= (CheckBox) findViewById(R.id.gpiock_4);

        HardwareControler.exportGPIOPin(73);//使能端口
        HardwareControler.setGPIODirection(73, GPIOEnum.OUT);//设置GPIO为输出模式
        HardwareControler.exportGPIOPin(74);//使能端口
        HardwareControler.setGPIODirection(74, GPIOEnum.OUT);//设置GPIO为输出模式
        HardwareControler.exportGPIOPin(75);//使能端口
        HardwareControler.setGPIODirection(75, GPIOEnum.OUT);//设置GPIO为输出模式
        HardwareControler.exportGPIOPin(76);//使能端口
        HardwareControler.setGPIODirection(76, GPIOEnum.OUT);//设置GPIO为输出模式
        HardwareControler.setGPIOValue(73,GPIOEnum.LOW);
        HardwareControler.setGPIOValue(74,GPIOEnum.LOW);
        HardwareControler.setGPIOValue(75,GPIOEnum.LOW);
        HardwareControler.setGPIOValue(76,GPIOEnum.LOW);

        for(int i=0;i<4;i++)
        {
            gpioBox[i].setOnCheckedChangeListener(this);
            gpioButton[i].setOnClickListener(this);
            gpioSpinner[i].setOnItemSelectedListener(this);

            gpioLayout[i].setVisibility(View.GONE);
            gpioButton[i].setClickable(false);
            gpioDir[i]=gpioOUT;
            gpioSta[i]=gpioLOW;
            gpioInUsed[i]=false;
            lastgpioInSta[i]=-1;
        }

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x101) {
                    if (msg.getData().getInt("gpio1") == GPIOEnum.LOW) {
                        gpioView[0].setText("低电平");
                    } else {
                        gpioView[0].setText("高电平");
                    }
                    if(msg.getData().getInt("gpio2")  == GPIOEnum.LOW) {
                        gpioView[1].setText("低电平");
                    } else {
                        gpioView[1].setText("高电平");
                    }
                    if (msg.getData().getInt("gpio3")  == GPIOEnum.LOW) {
                        gpioView[2].setText("低电平");
                    } else {
                        gpioView[2].setText("高电平");
                    }
                    if (msg.getData().getInt("gpio4")  == GPIOEnum.LOW) {
                        gpioView[3].setText("低电平");
                    } else {
                        gpioView[3].setText("高电平");
                    }
                }
                super.handleMessage(msg);
            }
        };
        t.start();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.gpiobtn_1:
            {
                if (gpioDir[0]==gpioOUT)
                {
                    gpioSta[0]=!gpioSta[0];
                    if(gpioSta[0]==gpioLOW)
                    {
                        HardwareControler.setGPIOValue(73,GPIOEnum.LOW);
                        gpioView[0].setText("低电平");
                    }
                    else
                    {
                        HardwareControler.setGPIOValue(73,GPIOEnum.HIGH);
                        gpioView[0].setText("高电平");
                    }
                }
            }
            break;
            case R.id.gpiobtn_2:
            {
                if (gpioDir[1]==gpioOUT)
                {
                    gpioSta[1]=!gpioSta[1];
                    if(gpioSta[1]==gpioLOW)
                    {
                        HardwareControler.setGPIOValue(74,GPIOEnum.LOW);
                        gpioView[1].setText("低电平");
                    }
                    else
                    {
                        HardwareControler.setGPIOValue(74,GPIOEnum.HIGH);
                        gpioView[1].setText("高电平");
                    }
                }
            }
            break;
            case R.id.gpiobtn_3:
            {
                if (gpioDir[2]==gpioOUT)
                {
                    gpioSta[2]=!gpioSta[2];
                    if(gpioSta[2]==gpioLOW)
                    {
                        HardwareControler.setGPIOValue(75,GPIOEnum.LOW);
                        gpioView[2].setText("低电平");
                    }
                    else
                    {
                        HardwareControler.setGPIOValue(75,GPIOEnum.HIGH);
                        gpioView[2].setText("高电平");
                    }
                }
            }
            break;
            case R.id.gpiobtn_4:
            {
                if (gpioDir[3]==gpioOUT)
                {
                    gpioSta[3]=!gpioSta[3];
                    if(gpioSta[3]==gpioLOW)
                    {
                        HardwareControler.setGPIOValue(76,GPIOEnum.LOW);
                        gpioView[3].setText("低电平");
                    }
                    else
                    {
                        HardwareControler.setGPIOValue(76,GPIOEnum.HIGH);
                        gpioView[3].setText("高电平");
                    }
                }
            }
            break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId())
        {
            case R.id.gpiodir_1:
            {
                if(gpioInUsed[0]==true) {
                    if (position == 0) {
                        HardwareControler.setGPIODirection(73, GPIOEnum.OUT);
                        HardwareControler.setGPIOValue(73,GPIOEnum.LOW);
                        gpioButton[0].setVisibility(View.VISIBLE);
                        gpioButton[0].setClickable(true);
                        gpioSta[0]=gpioLOW;
                        gpioDir[0]=gpioOUT;
                    } else {
                        HardwareControler.setGPIODirection(73, GPIOEnum.IN);
                        gpioButton[0].setVisibility(View.GONE);
                        gpioButton[0].setClickable(false);
                        gpioDir[0]=gpioIN;
                    }
                }
            }
            break;
            case R.id.gpiodir_2:
            {
                if(gpioInUsed[1]==true) {
                    if (position == 0) {
                        HardwareControler.setGPIODirection(74, GPIOEnum.OUT);
                        HardwareControler.setGPIOValue(74,GPIOEnum.LOW);
                        gpioButton[1].setVisibility(View.VISIBLE);
                        gpioButton[1].setClickable(true);
                        gpioSta[1]=gpioLOW;
                        gpioDir[1]=gpioOUT;
                    } else {
                        HardwareControler.setGPIODirection(74, GPIOEnum.IN);
                        gpioButton[1].setVisibility(View.GONE);
                        gpioButton[1].setClickable(false);
                        gpioDir[1]=gpioIN;
                    }
                }
            }
            break;
            case R.id.gpiodir_3:
            {
                if(gpioInUsed[2]==true) {
                    if (position == 0) {
                        HardwareControler.setGPIODirection(75, GPIOEnum.OUT);
                        HardwareControler.setGPIOValue(75,GPIOEnum.LOW);
                        gpioButton[2].setVisibility(View.VISIBLE);
                        gpioButton[2].setClickable(true);
                        gpioSta[2]=gpioLOW;
                        gpioDir[2]=gpioOUT;
                    } else {
                        HardwareControler.setGPIODirection(75, GPIOEnum.IN);
                        gpioButton[2].setVisibility(View.GONE);
                        gpioButton[2].setClickable(false);
                        gpioDir[2]=gpioIN;
                    }
                }
            }
            break;case R.id.gpiodir_4:
        {
            if(gpioInUsed[3]==true) {
                if (position == 0) {
                    HardwareControler.setGPIODirection(76, GPIOEnum.OUT);
                    HardwareControler.setGPIOValue(76,GPIOEnum.LOW);
                    gpioButton[3].setVisibility(View.VISIBLE);
                    gpioButton[3].setClickable(true);
                    gpioSta[3]=gpioLOW;
                    gpioDir[3]=gpioOUT;
                } else {
                    HardwareControler.setGPIODirection(76, GPIOEnum.IN);
                    gpioButton[3].setVisibility(View.GONE);
                    gpioButton[3].setClickable(false);
                    gpioDir[3]=gpioIN;
                }
            }
        }
        break;


        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked)
        {
            switch (buttonView.getId())
            {
                case R.id.gpiock_1:
                {
                    gpioLayout[0].setVisibility(View.VISIBLE);
                    gpioButton[0].setClickable(true);
                    gpioInUsed[0]=true;

                }
                break;
                case R.id.gpiock_2:
                {
                    gpioLayout[1].setVisibility(View.VISIBLE);
                    gpioButton[1].setClickable(true);
                    gpioInUsed[1]=true;
                }
                break;
                case R.id.gpiock_3:
                {
                    gpioLayout[2].setVisibility(View.VISIBLE);
                    gpioButton[2].setClickable(true);
                    gpioInUsed[2]=true;
                }
                break;
                case R.id.gpiock_4:
                {
                    gpioLayout[3].setVisibility(View.VISIBLE);
                    gpioButton[3].setClickable(true);
                    gpioInUsed[3]=true;
                }
                break;
            }
        }
        else
        {
            switch (buttonView.getId())
            {
                case R.id.gpiock_1:
                {
                    gpioLayout[0].setVisibility(View.GONE);
                    gpioButton[0].setClickable(false);
                    gpioInUsed[0]=false;
                    gpioSta[0]=gpioLOW;
                    gpioDir[0]=gpioOUT;
                    HardwareControler.setGPIODirection(73, GPIOEnum.OUT);
                    HardwareControler.setGPIOValue(73,GPIOEnum.LOW);
                    gpioView[0].setText("低电平");
                    gpioSpinner[0].setSelection(0);
                }
                break;
                case R.id.gpiock_2:
                {
                    gpioLayout[1].setVisibility(View.GONE);
                    gpioButton[1].setClickable(false);
                    gpioInUsed[1]=false;
                    gpioSta[1]=gpioLOW;
                    gpioDir[1]=gpioOUT;
                    HardwareControler.setGPIODirection(74, GPIOEnum.OUT);
                    HardwareControler.setGPIOValue(74,GPIOEnum.LOW);
                    gpioView[1].setText("低电平");
                    gpioSpinner[1].setSelection(0);
                }
                break;
                case R.id.gpiock_3:
                {
                    gpioLayout[2].setVisibility(View.GONE);
                    gpioButton[2].setClickable(false);
                    gpioInUsed[2]=false;
                    gpioSta[2]=gpioLOW;
                    gpioDir[2]=gpioOUT;
                    HardwareControler.setGPIODirection(75, GPIOEnum.OUT);
                    HardwareControler.setGPIOValue(75,GPIOEnum.LOW);
                    gpioView[2].setText("低电平");
                    gpioSpinner[2].setSelection(0);
                }
                break;
                case R.id.gpiock_4:
                {
                    gpioLayout[3].setVisibility(View.GONE);
                    gpioButton[3].setClickable(false);
                    gpioInUsed[3]=false;
                    gpioSta[3]=gpioLOW;
                    gpioDir[3]=gpioOUT;
                    HardwareControler.setGPIODirection(76, GPIOEnum.OUT);
                    HardwareControler.setGPIOValue(76,GPIOEnum.LOW);
                    gpioView[3].setText("低电平");
                    gpioSpinner[3].setSelection(0);
                }
                break;
            }
        }
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted())
        {
            if(gpioInUsed[0]||gpioInUsed[1]||gpioInUsed[2]||gpioInUsed[3])
            {
                if(!(gpioDir[0]&&gpioDir[1]&&gpioDir[2]&&gpioDir[3]))
                {
                    Message m=handler.obtainMessage();
                    int[] gpioInSta=new int[4];
                    for(int i=0;i<4;i++)
                    {
                        if(gpioInUsed[i]==true)
                        {
                            if(gpioDir[i]==gpioIN)
                            {
                                gpioInSta[i]=HardwareControler.getGPIOValue(73 + i);
                            }
                        }
                        else
                        {
                            gpioInSta[i]=-1;
                        }
                    }
                    Bundle bundle =new Bundle();
                    bundle.putInt("gpio1",gpioInSta[0]);
                    bundle.putInt("gpio2",gpioInSta[1]);
                    bundle.putInt("gpio3",gpioInSta[2]);
                    bundle.putInt("gpio4",gpioInSta[3]);
                    m.setData(bundle);
                    m.what=0x101;
                    if(lastgpioInSta[0]!=gpioInSta[0]||lastgpioInSta[1]!=gpioInSta[1]||lastgpioInSta[2]!=gpioInSta[2]||lastgpioInSta[3]!=gpioInSta[3])
                    {
                        handler.sendMessage(m);
                    }
                    for(int i=0;i<4;i++) {
                        lastgpioInSta[i]=gpioInSta[i];
                    }
                }
            }
        }
    }
}
